package com.fengye.spring;

import com.fengye.revoker.RevokerFactoryBean;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.w3c.dom.Element;

/**
 * @Author fengye
 * @Date 2023/2/14 14:20
 **/
public class RevokerFactoryBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
    private final static Logger logger = LoggerFactory.getLogger(RevokerFactoryBeanDefinitionParser.class);

    @Override
    protected Class getBeanClass(Element element) {
        return RevokerFactoryBean.class;
    }
    @Override
    protected void doParse(Element element, BeanDefinitionBuilder bean) {
        try {
            String timeout = element.getAttribute("timeout");
            String targetInterface = element.getAttribute("interface");
            String clusterStrategy = element.getAttribute("clusterStrategy");
            String remoteAppKey = element.getAttribute("remoteAppKey");
            String appKey = element.getAttribute("appKey");
            String groupName = element.getAttribute("groupName");

            bean.addPropertyValue("timeout", timeout);
            bean.addPropertyValue("remoteAppKey", remoteAppKey);
            bean.addPropertyValue("appKey", appKey);

            bean.addPropertyValue("targetInterface", Class.forName(targetInterface));
            if(StringUtils.isNotBlank(clusterStrategy)) {
                bean.addPropertyValue("clusterStrategy", clusterStrategy);
            }
            if(StringUtils.isNotBlank(groupName)) {
                bean.addPropertyValue("groupName", groupName);
            }

        } catch (Exception e) {
            logger.error("RevokerFactoryBeanDefinitionParser is error", e);
            throw new RuntimeException(e);
        }

    }
}
